/*
 * Change Logs:
 * Date           Author       Notes
 * 2020-10-30     chenbin      small crond the first version
 */
#ifndef _SMALL_CROND_H_
#define _SMALL_CROND_H_

#include "stdint.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <rtthread.h>
#include <ctype.h>

#define LINE_NUM_MAX 256
#define LINE_TEXT_MAX 128

typedef struct cron_line
{
	uint8_t text_length;
	uint8_t cmd_length;
	char text[LINE_TEXT_MAX];    /* text */
	char *cmd;                   /* shell command */
	/* ordered by size, not in natural order. makes code smaller: */
	char Dow[7];                 /* 0-6, beginning sunday */
	char Mons[12];               /* 0-11 */
	char Hrs[24];                /* 0-23 */
	char Days[32];               /* 1-31 */
	char Mins[60];               /* 0-59 */
	char Secs[60];               /* 0-59 */
	
	
//	uint8_t		second[8];			/* 0-59  */
//	uint8_t		minute[8];			/* 0-59  */
//	uint8_t		hour[3];				/* 0-23  */
//	uint8_t		week[1];				/* 1-7  */
//	uint8_t		day[4];					/* 1-32 */
//	uint8_t		month[2];				/*  */
//	uint32_t	year[2];				/*  */
	
	
	uint32_t run_count;
	uint32_t run_flag;
	int ret_code;
} cron_line_t;

typedef struct cron
{
	uint16_t status;  //
	uint16_t run_tick;
	uint8_t line_max;
	uint8_t line_used;
	int(*run)(char *cmd, uint32_t length);
	cron_line_t *line_array;
} small_cron_t;

cron_line_t *cron_line_get(small_cron_t *cron,uint8_t index);

int cron_line_load(small_cron_t *cron,const char* text,uint32_t length);

int cron_line_unload(small_cron_t *cron,uint8_t index);

int cron_line_reload(small_cron_t *cron,uint8_t index,const char* text,uint32_t length);

int cron_line_max(small_cron_t *cron);
int cron_line_used(small_cron_t *cron);
int cron_line_clear(small_cron_t *cron);

int cron_line_isused(small_cron_t *cron,uint8_t index);

int cron_text_load(small_cron_t *cron,const char* text,uint32_t length);
int cron_text_reload(small_cron_t *cron,const char* text,uint32_t length);

int cron_jobs_check(small_cron_t *cron,time_t last_time,time_t now_time);
int cron_jobs_run(small_cron_t *cron);


#endif /* _SMALL_CROND_H_ */
